var app = {

	readyCallback: null,
	pageControllers: [],

    // Application Constructor
    initialize: function(callback) {
		readyCallback = callback;
        this.bindEvents();
    },

    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        console.log('Received Event: ' + id);
		if (readyCallback)
			readyCallback();
    },

	// RegisterPageControllers via key
	registerPageController: function (key, controller) {
		this.pageControllers[key] = controller;
	},

	// get page controller via key
	getPageController: function (key) {
		return this.pageControllers[key];
	},

	// Raise device ready event on each controllers
	raiseDeviceReadyEventOnControllers: function (callback) {
		var cCount = 0;

		for (var key in this.pageControllers) {
			var controller = this.pageControllers[key];
			if (!controller)
				continue;

			if (!controller.init)
				continue;

			if (typeof controller.init !== 'function')
				continue;

			cCount++;
			controller.init(function () {
				if (--cCount > 0)
					return;

				if (callback)
					callback();
			});
		}
	},

	// load page from url to body
	loadPage: function (url, callback) {
		$.ajax(url, {
			method: 'GET',
			isLocal: true,
			dataType: 'text',
			success: function (html) {
				$('body').append(html);
				if (callback)
					callback();
			},
			error: function () {
				console.error('Could not load page content from url: ' + url);
			}
		});
	},

	// unload page from body via selector
	unloadPage: function (selector) {
		var page = $(selector);
		$(body).removeChild(page);
	},

	// after load configuration
	// 		range		configuration action range
	afterLoad: function(range) {
		// configure the after load css settings
		for (var i=0; i<cssSettingFuncs.length; i++) {
			var func = cssSettingFuncs[i];
			if (func)
				func(range);
		}
	},

};

/** Main entrance of application **/
$(document).ready(function() {
	app.initialize(function() {
		//CssToolKits.setHeightMatchParent($('html'), $(window));
		document.oncontextmenu = function () { return false; };
		var splash = $('body').children();
		app.raiseDeviceReadyEventOnControllers(function () {
			PlatformSupport_init();

			var mainPageController = app.getPageController('p0000');
			if (!mainPageController) {
				alert('Default page not set.');
				return;
			}

			setTimeout(function() {
				splash.remove();
				mainPageController.show();
				if (!PlatformSupport.isPasswordSet())
					p0003Controller.showDialog(true);
				else {
					p0004Controller.showDialog(function (cancelled) {
						if (cancelled)
							PlatformSupport.exit();
					});
				}
			}, 2000);
		});
	});
});